package com.inspur.humantask.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.xcc.cache.CacheManager;

@Component
@Aspect
public class SaveformJsonAop {

	private Logger logger= LoggerFactory .getLogger(SaveformJsonAop.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	
	/*@Pointcut("execution(* com.inspur.humantask.service.SyncService.testKey(..))")
    public void pointCut() {
    }*/
	
	
	  public Object action(ProceedingJoinPoint pjp) throws Throwable {
		  
			 try {
				 Object[] args=pjp.getArgs();
					String mFromJsonDateSql="select * from scidc.form_data_info where id="+args[1];
					logger.info(mFromJsonDateSql);
					
					// String mFromJsonDateSql="select * from scidc.form_data_info where id=1965365257371648";
					 List<Map<String, Object>>  mFromJsonDate=jdbcTemplate.queryForList(mFromJsonDateSql);
					 CacheManager.put(args[1]+"", mFromJsonDate);
			 }catch (Exception e) {
					e.printStackTrace();
			}finally {
				return  pjp.proceed();
			}
			 
		
			 
		   
		  
	  }
	  List<Map<String, Object>>  mFromJsonDateOld = new ArrayList<>();
	   public Object uploadFile(ProceedingJoinPoint pjp) throws Throwable {
		   Object[] args=pjp.getArgs();
		 
		   Object str;
			 try {
				 String mFromJsonDateSql="select * from form_attachment_info where form_Data_id="+args[1];
				 logger.info(mFromJsonDateSql);
			
				 // String mFromJsonDateSql="select * from scidc.form_data_info where id=1965365257371648";
				 mFromJsonDateOld=jdbcTemplate.queryForList(mFromJsonDateSql);
				 //	CacheManager.put(args[1]+"file", mFromJsonDate);
			 }catch (Exception e) {
				 e.printStackTrace();
			 }finally {
				 str=	  pjp.proceed();
			 }
				
			 
		   
		   
		 
			
			
			
			
			 new Thread(()->{
	 			 
	 			 
		 			logger.info(Thread.currentThread().getName()+"后");
		 			//新老 文件
		 			 String mFromJsonDateSql="select * from form_attachment_info where form_Data_id="+args[1];
					 logger.info(mFromJsonDateSql);
				
					 List<Map<String, Object>>  mFromJsonDateNew=jdbcTemplate.queryForList(mFromJsonDateSql);
					 
						String flowIdSql="select * from scidc.form_data_info where id="+args[1];
						
						List<Map<String, Object>>  flowIdList=jdbcTemplate.queryForList(flowIdSql);
						
						String  processId=(String) flowIdList.get(0).get("flow_id");
					 
						String bussinKeySql= "select KEY_ from  scidc.act_re_procdef WHERE ID_=(select PROC_DEF_ID_ from scidc.ACT_HI_PROCINST  where PROC_INST_ID_= "+processId+")";
						System.out.println(bussinKeySql);
						String key=jdbcTemplate.queryForList(bussinKeySql, String.class).get(0);
						
						
				 
						String keyConfig="select * from scidc.t_dic_flow_log where dic_type='"+key+"'"+"and  dic_key='file'";
						System.out.println(keyConfig);
						List<Map<String, Object>> mconfigs=jdbcTemplate.queryForList(keyConfig) ;
					 
						
						if(mconfigs.size()<=0) {
							return;
						}
						
						
						//mFromJsonDate.get(0).get("attachment_name");
						String oldvalue="";
						String valueNew="";
						for(Map<String, Object> arg:mFromJsonDateOld) {
							oldvalue+=arg.get("attachment_name")+"\n";
						}
						
						for(Map<String, Object> arg:mFromJsonDateNew) {
							valueNew+=arg.get("attachment_name")+"\n";
						}
						
						String sql="insert into scidc.process_log (oldvalue,lastvalue,taskInfoId,assignId,fieldName,field,processInstanceId,fieldType,configId) values (\'"
								+ oldvalue+"\',\'"+valueNew+"\',"+args[1]+","+mFromJsonDateNew.get(0).get("userid")+",\'"+"上传文件"+"\',\'"+"file"+"\',\'"+processId+"\'"+","+"0"+","+mconfigs.get(0).get("id")+""
					+ ")";
						jdbcTemplate.execute(sql);
						System.out.println(sql);	
		 			
			 	}).start();
			return str;
	     }
	   String formId;
	   public Object delCommonFile(ProceedingJoinPoint pjp) throws Throwable {
		   Object[] args=pjp.getArgs();
		  
		   Object str;
			 try {
				 
				 
				 String sql="select form_Data_id from form_attachment_info where id="+args[0];
				 mFromJsonDateOld=jdbcTemplate.queryForList(sql);
				  formId=mFromJsonDateOld.get(0).get("form_Data_id")+"";
				 
				 String mFromJsonDateSql="select * from form_attachment_info where form_Data_id="+formId;
				 logger.info(mFromJsonDateSql);
			
				 // String mFromJsonDateSql="select * from scidc.form_data_info where id=1965365257371648";
				 mFromJsonDateOld=jdbcTemplate.queryForList(mFromJsonDateSql);
				 //	CacheManager.put(args[1]+"file", mFromJsonDate);
			 }catch (Exception e) {
				 e.printStackTrace();
			 }finally {
				 str=	  pjp.proceed();
			 }
				
			 
		   
		   
		 
			
			
			
			
			 new Thread(()->{
	 			 
	 			 
		 			logger.info(Thread.currentThread().getName()+"后");
		 			//新老 文件
		 			 String mFromJsonDateSql="select * from form_attachment_info where form_Data_id="+ formId;
					 logger.info(mFromJsonDateSql);
				
					 List<Map<String, Object>>  mFromJsonDateNew=jdbcTemplate.queryForList(mFromJsonDateSql);
					 
						String flowIdSql="select * from scidc.form_data_info where id="+formId;
						
						List<Map<String, Object>>  flowIdList=jdbcTemplate.queryForList(flowIdSql);
						
						String  processId=(String) flowIdList.get(0).get("flow_id");
					 
						String bussinKeySql= "select KEY_ from  scidc.act_re_procdef WHERE ID_=(select PROC_DEF_ID_ from scidc.ACT_HI_PROCINST  where PROC_INST_ID_= "+processId+")";
						System.out.println(bussinKeySql);
						String key=jdbcTemplate.queryForList(bussinKeySql, String.class).get(0);
						
						
				 
						String keyConfig="select * from scidc.t_dic_flow_log where dic_type='"+key+"'"+"and  dic_key='file'";
						System.out.println(keyConfig);
						List<Map<String, Object>> mconfigs=jdbcTemplate.queryForList(keyConfig) ;
					 
						
						if(mconfigs.size()<=0) {
							return;
						}
						
						
						//mFromJsonDate.get(0).get("attachment_name");
						String oldvalue="";
						String valueNew="";
						for(Map<String, Object> arg:mFromJsonDateOld) {
							oldvalue+=arg.get("attachment_name")+"\n";
						}
						
						for(Map<String, Object> arg:mFromJsonDateNew) {
							valueNew+=arg.get("attachment_name")+"\n";
						}
						
						 CacheManager.put(formId+"fileOld", oldvalue);
						 CacheManager.put(formId+"fileNew", valueNew);
					/*	String sql="insert into scidc.process_log (oldvalue,lastvalue,taskInfoId,assignId,fieldName,field,processInstanceId,fieldType,configId) values (\'"
								+ oldvalue+"\',\'"+valueNew+"\',"+formId+","+mFromJsonDateNew.get(0).get("userid")+",\'"+"上传文件"+"\',\'"+"file"+"\',\'"+processId+"\'"+","+"0"+","+mconfigs.get(0).get("id")+""
					+ ")";
						jdbcTemplate.execute(sql);*/
					//	System.out.println(sql);	
		 			
			 	}).start();
			return str;
	     }
	
}
